package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private AddressDao addressDao;
    //添加
    public void add(Member member) {
        memberDao.add(member);
    }




    //根据手机号查询
    public Member findByTelephone(String telephone) {
        return  memberDao.findByTelephone(telephone);
    }


    public List<Integer> findMemberCountByMonth(List<String> list) {
        List<Integer> members =new ArrayList<>();
        for (String s : list) {
            String mouth = s+"-31";
            Integer count = memberDao.findMemberCountByMonth(s);
            members.add(count);
        }
        return members;
    }

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> members =memberDao.selectByCondition(queryString);
        long total = members.getTotal();
        List<Member> result = members.getResult();
        for (Member member : result) {
            Date date = member.getBirthday();
            if(date != null) {
                String birthday = new SimpleDateFormat("yyyy").format(date);
                int birthdayYear = Integer.parseInt(birthday);
                String today = new SimpleDateFormat("yyyy").format(new Date());
                int todayYear = Integer.parseInt(today);
                if (birthdayYear != 0 && birthdayYear < todayYear) {
                    member.setAge(todayYear - birthdayYear );
                }
            }
            }
        return new PageResult(total,result);
    }

    //根据id号查询
    public Member findById(Integer id) {

        return memberDao.findById(id);
    }

    //修改
    public void edit(Member member) {
        memberDao.edit(member);
    }

    //删除
    public void delete(Integer id) {
        Long count = orderDao.findCountByMemberId(id);
        if(count > 0){
            Order order = orderDao.findOrderByMemberId(id);
            addressDao.deleteByOrderId(order.getId());
            orderDao.deleteByMemberId(id);
        }
        memberDao.deleteById(id);
    }

    @Override
    public List<Integer> findAgeNumber() {
        /*如按0-6岁为婴幼儿、7-12岁为少儿、13-17岁为青少年、
        18-45岁为青年、46-69岁为中年、>69岁为老年，*/
        List<Date> members = memberDao.findAge();
        int age = 0;
        int baby = 0;
        int boy = 0 ;
        int young = 0 ;
        int youngman = 0 ;
        int man = 0 ;
        int oldman = 0 ;

        for (Date date : members) {
            if(date != null) {
                String birthday = new SimpleDateFormat("yyyy").format(date);
                int birthdayYear = Integer.parseInt(birthday);
                String today = new SimpleDateFormat("yyyy").format(new Date());
                int todayYear = Integer.parseInt(today);
                 age = (todayYear - birthdayYear);
            }
            if (age<6){
                baby++;
                continue;
            }
            if (age>6&&age<=12){
                boy++;
                continue;
            }
            if (age>12&&age<=17){
                young++;
                continue;
            }
            if (age>17&&age<=45){
                youngman++;
                continue;
            }
            if (age>45&&age<=69){
                man++;
                continue;
            }
            if (age>69){
                oldman++;
            }
        }
        List<Integer> list = new ArrayList<>();
        list.add(baby);
        list.add(boy);
        list.add(young);
        list.add(youngman);
        list.add(man);
        list.add(oldman);
        return list;
    }
}
